//
//  Map.m
//
//  Created by TuanNT on 5/4/14
//  Copyright (c) 2014 Fsoft. All rights reserved.
//

#import "Map.h"


NSString *const kMapID = @"ID";
NSString *const kMapSub = @"Sub";
NSString *const kMapLongitude = @"longitude";
NSString *const kMapFeature = @"Feature";
NSString *const kMapMain = @"Main";
NSString *const kMapAddress = @"Address";
NSString *const kMapPrice = @"Price";
NSString *const kMapName = @"Name";
NSString *const kMapImage = @"Image";
NSString *const kMapLatitude = @"latitude";
NSString *const kMapContent = @"Content";


@interface Map ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation Map

@synthesize iDProperty = _iDProperty;
@synthesize sub = _sub;
@synthesize longitude = _longitude;
@synthesize feature = _feature;
@synthesize main = _main;
@synthesize address = _address;
@synthesize price = _price;
@synthesize name = _name;
@synthesize image = _image;
@synthesize latitude = _latitude;
@synthesize content = _content;


+ (Map *)modelObjectWithDictionary:(NSDictionary *)dict
{
    Map *instance = [[Map alloc] initWithDictionary:dict];
    return instance;
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.iDProperty = [self objectOrNilForKey:kMapID fromDictionary:dict];
            self.sub = [self objectOrNilForKey:kMapSub fromDictionary:dict];
            self.longitude = [self objectOrNilForKey:kMapLongitude fromDictionary:dict];
            self.feature = [self objectOrNilForKey:kMapFeature fromDictionary:dict];
            self.main = [self objectOrNilForKey:kMapMain fromDictionary:dict];
            self.address = [self objectOrNilForKey:kMapAddress fromDictionary:dict];
            self.price = [self objectOrNilForKey:kMapPrice fromDictionary:dict];
            self.name = [self objectOrNilForKey:kMapName fromDictionary:dict];
            self.image = [self objectOrNilForKey:kMapImage fromDictionary:dict];
            self.latitude = [self objectOrNilForKey:kMapLatitude fromDictionary:dict];
            self.content = [self objectOrNilForKey:kMapContent fromDictionary:dict];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.iDProperty forKey:kMapID];
    [mutableDict setValue:self.sub forKey:kMapSub];
    [mutableDict setValue:self.longitude forKey:kMapLongitude];
    [mutableDict setValue:self.feature forKey:kMapFeature];
    [mutableDict setValue:self.main forKey:kMapMain];
    [mutableDict setValue:self.address forKey:kMapAddress];
    [mutableDict setValue:self.price forKey:kMapPrice];
    [mutableDict setValue:self.name forKey:kMapName];
    [mutableDict setValue:self.image forKey:kMapImage];
    [mutableDict setValue:self.latitude forKey:kMapLatitude];
    [mutableDict setValue:self.content forKey:kMapContent];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.iDProperty = [aDecoder decodeObjectForKey:kMapID];
    self.sub = [aDecoder decodeObjectForKey:kMapSub];
    self.longitude = [aDecoder decodeObjectForKey:kMapLongitude];
    self.feature = [aDecoder decodeObjectForKey:kMapFeature];
    self.main = [aDecoder decodeObjectForKey:kMapMain];
    self.address = [aDecoder decodeObjectForKey:kMapAddress];
    self.price = [aDecoder decodeObjectForKey:kMapPrice];
    self.name = [aDecoder decodeObjectForKey:kMapName];
    self.image = [aDecoder decodeObjectForKey:kMapImage];
    self.latitude = [aDecoder decodeObjectForKey:kMapLatitude];
    self.content = [aDecoder decodeObjectForKey:kMapContent];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_iDProperty forKey:kMapID];
    [aCoder encodeObject:_sub forKey:kMapSub];
    [aCoder encodeObject:_longitude forKey:kMapLongitude];
    [aCoder encodeObject:_feature forKey:kMapFeature];
    [aCoder encodeObject:_main forKey:kMapMain];
    [aCoder encodeObject:_address forKey:kMapAddress];
    [aCoder encodeObject:_price forKey:kMapPrice];
    [aCoder encodeObject:_name forKey:kMapName];
    [aCoder encodeObject:_image forKey:kMapImage];
    [aCoder encodeObject:_latitude forKey:kMapLatitude];
    [aCoder encodeObject:_content forKey:kMapContent];
}


@end
